<template>
	<view class="content">
		<u-empty v-if="concernList.length <= 0" mode="list" width="50%" textSize="30"
			icon="http://cdn.uviewui.com/uview/empty/list.png">
		</u-empty>
		<view class="concern" v-else v-for="(item,index) in concernList" :key="index">
			<view class="concern-list">
				<image @click="gohomepage(item.focusUid)"
					:src=" item.avatar? $u.fileBaseUrl + item.avatar :'../../static/images/me/default-avatar-orange.png' "
					mode="aspectFill"></image>
				<view class="concern-center">
					<text>{{item.nickname}}</text>
					<text>{{item.title}}</text>
				</view>
			</view>
			<button @click="getfocusOrCancelFocusUser(item)">取消关注</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				concernList: [],
				page: {
					pageNum: 1,
					pageSize: 10,
				},
				total: 0
			};
		},
		methods: {
			// 获取关注列表
			async getuserFocus() {
				let res = await this.$u.api.getuserFocus(this.page)
				if (res.code == 200) {
					// 如果是第一页，直接替换数据
					if (this.page.pageNum == 1) {
						this.concernList = res.rows;
					} else {
						// 如果不是第一页，追加数据
						this.concernList = [...this.concernList, ...res.rows];
					}
					this.total = res.total;
				}
			},
			// 关注或取消关注
			async getfocusOrCancelFocusUser(item) {
				let res = await this.$u.api.getfocusOrCancelFocusUser({
					focusId: item.focusUid,
					type: 0,
				})
				if (res.code == 200) {
					this.$u.toast('取消关注成功')
					this.getuserFocus()
				}
			},
			// 跳转到关注主页
			gohomepage(focusUid) {
				console.log(focusUid);
				uni.navigateTo({
					url: `/packagesMe/me/homepage?focusUid=${focusUid}`
				})
			},
			// 下拉刷新
			onPullDownRefresh() {
				// 刷新数据的逻辑
				this.page.pageNum = 1
				this.page.pageSize = 10
				this.getuserFocus()
				// 模拟异步操作
				setTimeout(() => {
					// 刷新完成后，结束下拉刷新
					uni.stopPullDownRefresh();
				}, 1000);
			},
			// 上拉加载
			onReachBottom() {
				this.loadMore();
			},
			// 上拉加载
			loadMore() {
				// 模拟异步加载数据
				if (this.concernList.length >= this.total) {
					uni.showToast({
						title: '没有更多关注了',
						icon: "none"
					});
					return;
				}

				this.page.pageNum += 1;
				uni.showLoading({
					title: '加载中...'
				});

				this.getuserFocus().then(() => {
					uni.hideLoading();
				}).catch(error => {
					console.error('加载更多数据时出错:', error);
					uni.hideLoading();
					uni.showToast({
						title: '加载失败，请重试',
						icon: 'none'
					});
				});
			},
		},
		onLoad() {
			this.getuserFocus()
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		min-height: 95vh;
		background-color: #fff;
		padding: 30rpx;
		padding-left:48rpx;
		box-sizing: border-box;

		.concern {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 20rpx 0;

			.concern-list {
				display: flex;
				align-items: center;

				image {
					width: 110rpx;
					height: 110rpx;
					margin-right: 20rpx;
					border-radius: 50%;
				}

				.concern-center {
					text {
						display: block;

						&:nth-child(1) {
							font-size: 26rpx;
							color:#333;
						}

						&:nth-child(2) {
							font-size: 26rpx;
							color: #666666;
						}
					}
				}
			}

			button {
				font-size: 20rpx;
				color: #666666;
				padding: 10rpx 24rpx;
				background-color: #dadada;
				border-radius: 10rpx;
				box-sizing: border-box;
			}
		}
	}
</style>